package de.tabuma.pm.projectnavigation.logik;

import de.tabuma.pm.projectnavigation.entity.ProjectNavigation;
import de.tabuma.pm.logik.*;

import java.util.*;
import java.util.logging.*;

/**
  * @author $Author: $
  * @version $Revision: $
  * @since $Date: $
 */
public class ProjectNavigationFacade {

    private static final Logger log = Logger.getLogger(ProjectNavigationFacade.class.getName());

    public static int create(ProjectNavigation navigation, String user) {
        String keys[] = new String[1];
        keys[0] = "navigationId";
        String values[] = new String[1];
        values[0] = "" + navigation.getNavigationId();
        List<ProjectNavigation> pnList = PMF.get(keys, values, new ProjectNavigation());
        navigation.setOrder(pnList.size()+1);
        return (int) PMF.doCreate(navigation, user);
    }

    public static void update(ProjectNavigation navigation, String user) {
        PMF.doUpdate(navigation, user);
    }

    public static ProjectNavigation get(int navigationID) {
        return PMF.get(navigationID, new ProjectNavigation());
    }

    public static void remove(int navigationID, String user) {
        ProjectNavigation pn = get(navigationID);
        List<ProjectNavigation> pnList = getSubnavigationList(navigationID);
        for(int i=0;i<pnList.size();i++) {
            ProjectNavigation pn1 = pnList.get(i);
            remove(pn1.getNavigationId(), user);
        }
        PMF.doRemove(pn, user);
    }

    public static List<ProjectNavigation> getList(int projectID) {
        String keys[] = new String[1];
        keys[0] = "projectId";
        String values[] = new String[1];
        values[0] = ""+projectID;
        List<ProjectNavigation> pnList = PMF.get(keys, values, new ProjectNavigation());
        for(int i=0;i<pnList.size();i++) {
            for(int j=i+1;j<pnList.size();j++) {
                if(pnList.get(i).getOrder()>pnList.get(j).getOrder()) {
                    ProjectNavigation pn = pnList.get(j);
                    pnList.set(j, pnList.get(i));
                    pnList.set(i, pn);
                }
            }
        }
        return pnList;
    }

    public static List<ProjectNavigation> getSubnavigationList(int projectnavigationId) {
        List<ProjectNavigation> pnList = new ArrayList<ProjectNavigation>();
        String keys[] = new String[1];
        keys[0] = "navigationId";
        String values[] = new String[1];
        values[0] = ""+projectnavigationId;
        List<ProjectNavigation> pnList1 = PMF.get(keys, values, new ProjectNavigation());
        for(int i=0;i<pnList1.size();i++) {
            ProjectNavigation element = pnList1.get(i);
            pnList.add(element);
            List<ProjectNavigation> pnList2 = getSubnavigationList(element.getId().intValue());
            pnList.addAll(pnList2);
        }
        return pnList;
    }

}
